﻿//==============================================================================  
//Copyright (C) 2012-2015 9UN.ORG. All rights reserved. 
//GUID：b2096c56-e0c2-490e-bbba-2242446de6cc
//CLR Version: 4.0.30319.18033
//Code Author：Kevin Wang
//Contact：Email(Admin@9un.org),QQ(265382 or 74344)
//Filename：MatchContentType
//Namespace：RuanRang.Core.Utility
//Functions：MatchContentType  
//Created by Kevin Wang at 2013/2/4 20:33:26 http://blog.9un.org
//============================================================================== 

using System;
using System.Windows.Forms;
using Edu24ol.Framework.Utility.Interop;

namespace RuanRang.Core.Utility
{
    /// <summary>
    /// 为窗口启用Aero效果提供支持
    /// </summary>
    public class AeroHelper
    {
        /// <summary>
        /// 将窗口边框的Aero效果扩展到客户区域
        /// </summary>
        /// <param name="from">目标窗口</param>
        /// <param name="margin">外边距</param>
        /// <returns>成功与否</returns>
        public static bool ExtendGlassFrame(Form from, MARGINS margin)
        {
            if (!AeroGlassCompositionEnabled)
                return false;

            var hwnd = from.Handle;
            if (hwnd == IntPtr.Zero)
                throw new InvalidOperationException("在启用Aero效果前窗口必须已显示");
            //var margins = new MARGINS(margin.Left, margin.Top, margin.Right, margin.Bottom);
            NativeMethods.DwmExtendFrameIntoClientArea(hwnd, ref margin);

            return true;
        }

        /// <summary>
        /// 在窗口背后启用模糊效果
        /// </summary>
        /// <param name="from">目标窗口</param>
        /// <returns>成功与否</returns>
        public static bool EnableBlurBehindWindow(Form from)
        {
            if (!AeroGlassCompositionEnabled)
                return false;

            var hwnd = from.Handle;
            if (hwnd == IntPtr.Zero)
                throw new InvalidOperationException("在启用Aero效果前窗口必须已显示");

            //创建DWM_BLURBEHIND结构
            var bb = new DWM_BLURBEHIND
                {
                    dwFlags = DWM_BLURBEHIND.DWM_BB_ENABLE | DWM_BLURBEHIND.DWM_BB_BLURREGION,
                    fEnable = true,
                    hRegionBlur = NativeMethods.CreateRectRgn(0, 0, @from.Width, @from.Height)
                };

            NativeMethods.DwmEnableBlurBehindWindow(hwnd, ref bb);
            //回收句柄
            NativeMethods.DeleteObject(bb.hRegionBlur);
            return true;
        }

        /// <summary>
        /// 系统是否已启用Aero特效组合，若没启用，则不能使用Aero效果
        /// </summary>
        public static bool AeroGlassCompositionEnabled
        {
            get
            {
                //只有Windows Vista或更高版本才支持Aero特效
                if (Environment.OSVersion.Version.Major >= 6)
                {
                    try
                    {
                        return NativeMethods.DwmIsCompositionEnabled();
                    }
                    catch
                    {
                        return false;
                    }
                }
                return false;
            }
        }
    }
}